import datetime

class Person(object):
	def __init__(self, name):
		self.name = name
		self.birthday = None
		self.lastname = name.split(' ')[-1]

	def __str__(self):
		return self.name

	def __lt__(self, other):
		if self.lastname == other.lastname:
			return self.name < other.name
		return self.lastname < other.lastname
		# return self.getAge() < other.getAge()


	def getLastName(self):
		return self.lastname

	def setBirthday(self,year,month,day):
		self.birthday = datetime.date(year,month,day)

	def getAge(self):
		if self.birthday == None:
			raise ValueError
		return int((datetime.date.today() - self.birthday).days / 365)


# Definition of NeuPerson. It's the child class of Person.
class NeuPerson(Person):
	# This is a variable shared between Instance of NeuPerson.
	nextId = 0
	def __init__(self, name):
		Person.__init__(self, name)
		self.id = NeuPerson.nextId
		NeuPerson.nextId += 1

	def __str__(self):
		stu_id = self.getStuid()
		first_name = self.name.split(' ')[0]
		out_put = stu_id + first_name
		return out_put

	def __lt__(self, other):
		return self.id < other.id#排序从儿子看到爷爷去找---由此可以看出，它的孙子是按id排序

	def getIdnum(self):
		return self.id

	def speak(self, sentence):
		return (self.getLastName() + " says: " + sentence)

	def getStuid(self):
		return str(self.id).zfill(3)


# 学生类
class Student(NeuPerson):
	pass


#  本科生类
class UG(Student):
	def __init__(self, name, classYear):
		NeuPerson.__init__(self, name)
		self.year = classYear

	def getClass(self):
		return self.year

	def speak(self, utterance):
		return NeuPerson.speak(self, "Dude, " + utterance)

#  研究生类
class PG(Student):
	pass



def isStudent(obj):
	return isinstance(obj, Student)

#
class Grades(object):
	"""a mapping from student to a list of grades"""
	def __init__(self):
		"""Create empty grade book"""
		self.students = [] # list of Student objects
		self.grades = {} # maps idNum -> list of grades--字典
		self.isSorted = True # if self.students is sorted--标志位用于记录是否有排序

	def addStudent(self, student):
		"""Assumes: student is a type of Student
		Add student to the grade book"""
		if student in self.students:
			raise ValueError('Dupicate Student')#如果学生重复就会抛出异常
		self.students.append(student)#不重复所以把它存到列表
		self.grades[student.getIdnum()] = []#先不写成绩，所以=[]
		self.isSorted = False#因为没有排序所以=false

	def addGrade(self, student, grade):#写入成绩
		"""Assumes grade is a float
		Add grade to the list of the grade for students"""
		try:
			self.grades[student.getIdnum()].append(grade)
		except KeyError:#学生不在成绩单内不能输出成绩
			raise ValueError('Student is not in the Gradebook')

	def getGrades(self, student):
		"""Return a list of grades for the student"""
		try:
			return self.grades[student.getIdnum()][:]
		except KeyError:
			raise ValueError('Student is not in the Gradebook')

	def allStudents(self):
		"""Return a list of students in the grade book"""
		if not self.isSorted:
			self.students.sort()
			self.isSorted = True
		return self.students[:]#返回学生信息到函数外

def gradeReport(course):
	"Assumes :course is of type grades"
	report = []

	for s in course.allStudents():
		tot = 0.0#存总分
		numGrades = 0
		for g in course.getGrades(s):
			tot += g
			numGrades += 1
		try:
			average = tot/numGrades
			report.append(str(s) + '\'s mean grade is '+ str(average))
		except ZeroDivisionError:
			report.append(str(s) + ' has no grades')
	return '\n'.join(report)
# #
# #
ug1 = UG('Matt Damon',2018)
ug2 = UG('Ben Affleck',2019)
ug3 = UG('Drew Houston',2017)
ug4 = UG('Mark Zuckerberg',2017)
pg1 = PG('Bill Gates')
pg2 = PG('steve Wozniak')
six00 = Grades()
six00.addStudent(pg1)#将学生姓名加到成绩单
six00.addStudent(ug2)
six00.addStudent(ug1)
six00.addStudent(pg2)
six00.addStudent(ug4)
six00.addStudent(ug3)



six00.addGrade(pg1, 100,)#将学生成绩加到成绩单
six00.addGrade(pg2, 25)
six00.addGrade(ug1, 95)
six00.addGrade(ug2, 85)
six00.addGrade(ug3, 75)

print(gradeReport(six00))

#排序从儿子看到爷爷去找---由此可以看出，它的孙子是按id排序


sign="!"
l=["a","b","c"]
print(sign.join(l))